package tongbu方法;

public class test1 {
    public static void main(String[] args) {
        tongbufangfa tb = new tongbufangfa();
        Thread t1 = new Thread(tb,"A线程");
        Thread t2 = new Thread(tb,"B线程");
        Thread t3 = new Thread(tb,"C线程");
        t1.start();
        t2.start();
        t3.start();
    }
}

class tongbufangfa implements Runnable{
    private int ticket = 5000;
    @Override
    public void run() {
        while (true){

            if (method()) break;
        }
    }

    private synchronized boolean method() { // 这里抽离成为方法，并且在返回值前面 添加 synchronized关键字，就是同步
        if(ticket <=0){
            return true;
        }
        System.out.println(Thread.currentThread().getName() + "第" + ticket + "张牌");
        ticket--;
        return false;
    }
}